package com.tanhua.server.controller;

import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.MovementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/movements")
public class MovementController {

    @Autowired
    private MovementService movementService;

    /**
     * 发布动态
     */
    @PostMapping
    public ResponseEntity movements(Movement movement,
                                    MultipartFile imageContent[]) throws IOException {
        movementService.publishMovement(movement,imageContent);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询我的动态
     * 查喜欢、点赞、评论数标记A
     */
    @GetMapping("/all")
    public ResponseEntity findByUserId(Long userId,
                                       @RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementService.findByUserId(userId,page,pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 分页查询好友动态A
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity findFriend(@RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer pagesize){

        PageResult pr=movementService.findFriend(page,pagesize);
        return ResponseEntity.ok(pr);

    }

    /**
     * 查询推荐动态A
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/recommend")
    public  ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pr=movementService.recommend(page,pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 查看动态详情A
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity findOne(@PathVariable String id){
        MovementsVo vo  = movementService.findOne(id);
        return ResponseEntity.ok(vo);
    }

    @GetMapping("/{id}/like")
    public ResponseEntity commend(@PathVariable String id){
        Integer commendCount=movementService.commend(id);
        return ResponseEntity.ok(commendCount);
    }

    @GetMapping("/{id}/dislike")
    public ResponseEntity uncommend(@PathVariable String id){
        Integer commendCount=movementService.uncommend(id);
        return ResponseEntity.ok(commendCount);
    }

    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable String id){
        Integer commendCount=movementService.love(id);
        return ResponseEntity.ok(commendCount);
    }

    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable String id){
        Integer commendCount=movementService.unlove(id);
        return ResponseEntity.ok(commendCount);
    }



}
